package float
{
   import command.DoFrameTween;
   import command.DoTweenMax;
   
   import flash.display.Bitmap;
   import flash.display.BitmapData;
   import flash.display.BlendMode;
   import flash.events.Event;
   
   import jp.progression.casts.CastMovieClip;
   import jp.progression.commands.display.AddChildAt;
   import jp.progression.commands.display.RemoveChild;

   /**
    * This is a saturation edition.
    * @author  cjboy1984@gmail.com
    * @usage   see MyFloat.as
    */
   public class BaseFloat2 extends CastMovieClip
   {
      // blocker
      private var blocker:Bitmap;

      public function BaseFloat2( initObject:Object = null )
      {
         super( initObject );

         stop();
      }

      // --------------------- LINE ---------------------

      // ################### protected ##################

      override protected function atCastAdded():void
      {
         // blocker style
         blocker = new Bitmap(null, 'auto', true);
         blocker.blendMode = BlendMode.LAYER;
         blocker.bitmapData = new BitmapData(sw, sh, false, 0xFFFFFFFF);
         blocker.bitmapData.draw(stage);

         // init (x, y)
         onStageResize();

         // animation
         addCommand(
            new AddChildAt(parent, blocker, parent.getChildIndex(this)),
            new DoTweenMax(blocker, 0.2, { colorMatrixFilter:{saturation:0} } ),
            new DoFrameTween(this, totalFrames)
         );

         stage.addEventListener(Event.RESIZE, onStageResize);
      }

      override protected function atCastRemoved():void
      {
         stage.removeEventListener(Event.RESIZE, onStageResize);

         // animation
         addCommand(
            new DoFrameTween(this, 1),
            new DoTweenMax(blocker, 0.2, { colorMatrixFilter:{saturation:1} } ),
            new RemoveChild(parent, blocker)
         );
      }

      // #################### private ###################

      private function onStageResize(e:Event = null)
      {
         x = orgX;
         y = orgY;

         visible = false;
         blocker.bitmapData = new BitmapData(sw, sh, false);
         blocker.bitmapData.draw(stage);
         visible = true;
      }

      // Original x,y-pos
      private function get orgX():Number {	return sw / 2; }
      private function get orgY():Number {	return sh / 2; }

      private function get sw():Number { return stage.stageWidth; }
      private function get sh():Number { return stage.stageHeight; }

      // --------------------- LINE ---------------------

   }

}
